#include <iostream>
#include <ctime>
#include <vector>
#include <random>
#include <cstdlib>
#include "stdio.h"

void init(){
    std::srand(std::time(NULL));
}

void four_operate(int &_r, char &_s, std::vector<int> &a, std::vector<int> &b, std::vector<int> &corr_ans)
{
    int _a = std::rand() % _r;
    int _b = std::rand() % _r;
    int _ans;
    if (_s == '/') {
        _a = _b * (std::rand() % _r);
        _ans = _a / _b;
        printf("%d / %d = ", _a, _b);
    }
    else if (_s == '+') {
        _ans = _a + _b;
        printf("%d + %d = ", _a, _b);
    }
    else if (_s == '-') {
        _ans = _a - _b;
        printf("%d - %d = ", _a, _b);
    }
    else if (_s == '*') {
        _ans = _a * _b;
        printf("%d * %d = ", _a, _b);
    }
    a.push_back(_a);
    b.push_back(_b);
    corr_ans.push_back(_ans);
}

int main() {
    init();
    std::vector<int> a, b, corr_ans;  // 每次随机生成的两个变量,corr_ans:正确答案
    std::vector<int> ans;  // ans:用户输入的答案
    std::vector<char> ss;
    char sss[] = {'+','-','*','/'};

    int timeout = 10;  // 每道题目最大时间限制，单位s
    std::vector<int> ttime;  // 每道题用户所用时间

    int cnt = 0;
    std::cout << "请选择题目的数量：";
    while (std::cin >> cnt) {
        if (cnt > 0)
            break;
        else
            std::cerr << "请输入正确题目数量！" << std::endl;
    }

    int mode = -1;
    std::string smode[3] = {"easy", "medium", "hard"};
    printf("请确定题目难度：\neasy请选择0\nmedium请选择1\nhard请选择2\n");
    while (std::cin >> mode) {
        if (mode < 0 || mode > 2)
            std::cerr << "请选择正确难度！" << std::endl;
        else
            break;
    }

    int rrange = 0;
    std::cout << "请输入运算范围：";
    while (std::cin >> rrange) {
        if (rrange < 10) {
            std::cerr << "请输入大于10以上的运算范围!" << std::endl;
        }
        else
            break;
    }

    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "您有" << cnt << "道题需要解答, 难度模式为" << smode[mode] << std::endl;
    // std::cout << "开始生成题目中..." << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;


    int i = 0;
    int tmp_ans, si, t;
    char _s;
    std::time_t stime, etime; // 每道题起始、终止时间

    while (i < cnt)
    {
        if (mode==0)
        {
            si = std::rand() % 2;  // + or -
        }
        else if (mode==1)
        {
            si = std::rand() % 3;  // + or - or *
        }
        else if (mode==2)
        {
            si = std::rand() % 4; // + or - or * or /
        }
        _s = sss[si];
        ss.push_back(_s);

        four_operate(rrange, _s, a, b, corr_ans);

        stime = std::time(0);
        std::cin >> tmp_ans;
        etime = std::time(0);
        t = (int)(etime - stime);
        ttime.push_back(t);
        ans.push_back(tmp_ans);

        i++;
        std::cout << t << std::endl;
    }

    int num1 = 0, num2 = 0;
    int totalScore = cnt * 10;  // 总分
    int flag = 0;
    double totalTime = 0.0;
    double minTime = 999;
    for (int j = 0; j < cnt; ++j) {
        flag = 0;
        if (ans[j] != corr_ans[j]) {
            num1++;
            flag = 1;
            totalScore -= 10;
            printf("第%d题(%d %c %d)回答错误, 正确答案是：%d, 您的错误答案是：%d\n", j+1, a[j], ss[j], b[j], corr_ans[j], ans[j]);
        }

        if (ttime[j] > timeout) {
            num2++;
            if (flag == 0)
                totalScore -= 10;
            printf("第%d题(%d %c %d)回答超时,所用时间为%ds,超时了%ds\n", j+1, a[j], ss[j], b[j], ttime[j], ttime[j]-timeout);
        }

        totalTime += ttime[j];
        if (ttime[j] < minTime)
            minTime = ttime[j];

    }

    // total score is cnt * 10

    printf("一共%d道题,您答对了%d道,答错了%d道,超时了%d道\n", cnt, cnt-num1, num1, num2);
    printf("总共用时%fs,平均用时%fs,最短用时%fs\n", totalTime, totalTime/(1.0 * cnt), minTime);
    printf("最终得分是：%d\n", totalScore);

    return 0;
}